home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Utilities / MView / memdbg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2004-09-27  |  5.9 KB  |  250 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Copyright (C) 1999 Microsoft Corporation.  All Rights Reserved.
  4. //
  5. //  File:       memdbg.cpp
  6. //  Content:    D3DX memory debugging functions
  7. //
  8. ///////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "mviewpch.h"
  11.  
  12. #ifdef MEM_DEBUG
  13.  
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16.  
  17.  
  18. static UINT32 TotalAllocated = 0;       // Total allocated
  19. static UINT32 CurrentAllocation = 0;    // Current size of allocated memory
  20. static UINT32 MaxHeld = 0;          // Maximum size held at any point in time
  21. static UINT32 BlocksAllocated = 0;
  22.  
  23. struct SMemInfo
  24. {
  25.     SMemInfo    *pbNext;    
  26.     SMemInfo    *pbPrev;    
  27.     char        *szFilename;
  28.     UINT32      ulLine;
  29.     UINT32      ulAllocationSize;
  30.     UINT32      ulBlockNo;
  31. };
  32.  
  33. #define BLOCK_SIZE (sizeof(SMemInfo))
  34.  
  35. static SMemInfo *AllocationHead = NULL;
  36.  
  37. // User-defined operator new.
  38. void *operator new( size_t stAllocateBlock )
  39. {
  40.     static bool fInOpNew = 0;    // Guard flag.
  41.     UINT32 uiSize = 0;
  42.     BYTE *NewBlock;
  43.     SMemInfo *pmeminfo;
  44.  
  45.     if(!fInOpNew )
  46.     {
  47.         fInOpNew = 1;
  48.  
  49.         TotalAllocated += stAllocateBlock;
  50.  
  51.         CurrentAllocation += stAllocateBlock;
  52.  
  53.         if (CurrentAllocation > MaxHeld)
  54.             MaxHeld = CurrentAllocation;
  55.  
  56.         uiSize = stAllocateBlock;
  57.  
  58.         fInOpNew = 0;
  59.     }
  60.     else
  61.     {
  62.         // set size to 0 on non loggable blocks
  63.         uiSize = 0;
  64.     }
  65.  
  66.     NewBlock = (BYTE *)malloc( stAllocateBlock + sizeof(SMemInfo));
  67.     if (NewBlock == NULL)
  68.     {
  69.         return NULL;
  70.     }
  71.     else
  72.     {
  73.         pmeminfo = (SMemInfo *)NewBlock;
  74.         pmeminfo->ulAllocationSize = uiSize;
  75.         pmeminfo->szFilename = __FILE__;
  76.         pmeminfo->ulLine = __LINE__;
  77.         pmeminfo->pbNext = AllocationHead;
  78.         if (AllocationHead != NULL)
  79.             AllocationHead->pbPrev = (SMemInfo*)NewBlock;
  80.         pmeminfo->pbPrev = NULL;
  81.  
  82.         static int BlocksTest = 0xffffffff;
  83.         if (BlocksAllocated == BlocksTest)
  84.         {
  85.             BlocksAllocated = BlocksTest;
  86.         }
  87.  
  88.         pmeminfo->ulBlockNo = BlocksAllocated;
  89.         BlocksAllocated += 1;
  90.  
  91.         AllocationHead = (SMemInfo*)NewBlock;
  92.         return NewBlock + sizeof(SMemInfo);
  93.     }
  94.  
  95.  
  96. }
  97.  
  98. // User-defined operator new. (with file and line numbers)
  99. void *operator new(size_t stAllocateBlock, const UINT32 uiLineNumber, const char *szFilename)
  100. {
  101.     static bool fInOpNew = 0;    // Guard flag.
  102.     UINT32 uiSize = 0;
  103.     BYTE *NewBlock;
  104.     SMemInfo *pmeminfo;
  105.  
  106.     if(!fInOpNew )
  107.     {
  108.         fInOpNew = 1;
  109.  
  110.         //printf("Memory Allocated at %s:%d\n", szFilename, uiLineNumber);
  111.  
  112.         TotalAllocated += stAllocateBlock;
  113.  
  114.         CurrentAllocation += stAllocateBlock;
  115.  
  116.         if (CurrentAllocation > MaxHeld)
  117.             MaxHeld = CurrentAllocation;
  118.  
  119.         uiSize = stAllocateBlock;
  120.  
  121.         fInOpNew = 0;
  122.     }
  123.     else
  124.     {
  125.         // set size to 0 on non loggable blocks
  126.         uiSize = 0;
  127.     }
  128.  
  129.     NewBlock = (BYTE *)malloc(stAllocateBlock + sizeof(SMemInfo));
  130.     if (NewBlock == NULL)
  131.     {
  132.         return NULL;
  133.     }
  134.     else
  135.     {
  136.         pmeminfo = (SMemInfo *)NewBlock;
  137.         pmeminfo->ulAllocationSize = uiSize;
  138.         pmeminfo->szFilename = (char*)szFilename;
  139.         pmeminfo->ulLine = uiLineNumber;
  140.         pmeminfo->pbNext = AllocationHead;
  141.         if (AllocationHead != NULL)
  142.             AllocationHead->pbPrev = (SMemInfo*)NewBlock;
  143.         pmeminfo->pbPrev = NULL;
  144.         pmeminfo->ulBlockNo = BlocksAllocated;
  145.         BlocksAllocated += 1;
  146.  
  147.         AllocationHead = (SMemInfo*)NewBlock;
  148.         return NewBlock + sizeof(SMemInfo);
  149.     }
  150.  
  151.  
  152. }
  153.  
  154.  
  155.  
  156. // User-defined operator delete.
  157. void operator delete( void *pvMem )
  158. {
  159.     static fInOpDelete = 0;    // Guard flag.
  160.     BYTE *pbMem = (BYTE *)pvMem;
  161.     SMemInfo *pbNextElem, *pbPrevElem;
  162.  
  163.     if (pvMem == NULL)
  164.         return;
  165.  
  166.     pbMem -= BLOCK_SIZE;    // move back to the start of the allocated block
  167.  
  168.     pbPrevElem = ((SMemInfo*)pbMem)->pbPrev;
  169.     pbNextElem = ((SMemInfo*)pbMem)->pbNext;
  170.  
  171.     // if at the head, then remove the head element
  172.     if (pbPrevElem == NULL)
  173.     {
  174.         if (pbNextElem != NULL)
  175.         {
  176.             pbNextElem->pbPrev = NULL;
  177.         }
  178.  
  179.         AllocationHead = pbNextElem;
  180.     }
  181.     else
  182.     {
  183.         pbPrevElem->pbNext = pbNextElem;
  184.  
  185.         // NULL if the allocation is at the end of the list
  186.         if (pbNextElem != NULL)
  187.             pbNextElem->pbPrev = pbPrevElem;
  188.     }
  189.  
  190.     if( !fInOpDelete )
  191.     {
  192.         fInOpDelete = 1;
  193.  
  194.         CurrentAllocation -= ((SMemInfo *)pbMem)->ulAllocationSize;
  195.  
  196.         fInOpDelete = 0;
  197.     }
  198.  
  199. //  _ASSERT(_CrtIsMemoryBlock(pbMem), ((SMemInfo *)pbMem)->ulAllocationSize);
  200.     free(pbMem);
  201. }
  202.  
  203.  
  204. //
  205. // returns true if any memory was not freed
  206. //
  207. BOOL WINAPI D3DXDumpUnfreedMemoryInfo()
  208. {
  209.     SMemInfo *pbCurElem;
  210.     char *szFilename;
  211.     UINT32 uiLineNumber;
  212.     UINT32 uiSize;
  213.     UINT32 ulBlockNo;
  214.     char szBuf[512];
  215.  
  216.     // if no elements on list, then no leaks
  217.     if (AllocationHead == NULL)
  218.     {
  219.         return FALSE;
  220.     }
  221.  
  222.     MessageBox(NULL, "Look in debug output window for more info", "Memory Leaks!", MB_OK);
  223.  
  224.     pbCurElem = AllocationHead;
  225.  
  226.     while (pbCurElem != NULL)
  227.     {
  228.         szFilename = pbCurElem->szFilename;
  229.         uiLineNumber =  pbCurElem->ulLine;
  230.         uiSize = pbCurElem->ulAllocationSize;      
  231.         ulBlockNo = pbCurElem->ulBlockNo;
  232.  
  233.         sprintf(szBuf, "Memory Leak(%d):  %d bytes leaked at %s: %d\n", ulBlockNo, uiSize, szFilename, uiLineNumber);
  234.         OutputDebugString(szBuf);
  235.  
  236.         pbCurElem = ((SMemInfo*)pbCurElem)->pbNext;
  237.     }
  238.  
  239.     return TRUE;
  240. }
  241.  
  242. #else
  243.  
  244. BOOL WINAPI D3DXDumpUnfreedMemoryInfo()
  245. {
  246.     return FALSE;
  247. }
  248.  
  249. #endif
  250.